package com.qf.Web.Servlet;

import com.qf.Domain.*;
import com.qf.Service.CartsService;
import com.qf.Service.Impl.CartServiceImpl;
import com.qf.Service.Impl.OrderServiceImpl;
import com.qf.Service.Impl.UserServiceImpl;
import com.qf.Service.OrderService;
import com.qf.Service.UserService;
import com.qf.Utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
 *
 */
@WebServlet(name = "OrderServlet",value = "/orderservlet")
public class OrderServlet extends BaseServlet {
    //获取订单
    public String getOrderView(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        //获取购买的商品信息
        CartsService cartsService=new CartServiceImpl();
        List<Cart> carts = cartsService.fingById(user.getId());
        //获取地址信息
        UserService userService=new UserServiceImpl();
        try {
            List<Address> addList=userService.findAddress(user.getId());
            //放入域中
            request.setAttribute("carts",carts);
            request.setAttribute("addList",addList);
            return "/order.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "结账失败...");
            return "/message.jsp";
        }
    }
    //提交订单
    public String addOrder(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        String aid = request.getParameter("aid");
        if (StringUtils.isEmtry(aid)){
            request.setAttribute("msg", "提交订单失败");
            return "/message.jsp";
        }
        //查询购物车
        CartsService cartsService=new CartServiceImpl();
        List<Cart> carts = cartsService.fingById(user.getId());
        OrderService orderService=new OrderServiceImpl();
        try {
            if (carts!=null&&carts.size()>0) {
                Order order = orderService.addOrder(user.getId(), Integer.parseInt(aid), carts);//用户id，地址id，购物车
                request.setAttribute("order", order);
                return "/orderSuccess.jsp";
            }else {
                request.setAttribute("msg", "购物车为空");
                return "/message.jsp";
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    //查看订单
    public String getOrderList(HttpServletRequest request,HttpServletResponse response){
        //判断用户是否登录
        User  user = (User) request.getSession().getAttribute("user");
        OrderService orderService=new OrderServiceImpl();
        try {
            List<Order> orderList=orderService.findByOid(user.getId());
            request.setAttribute("orderList", orderList);
            return "/orderList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单失败");
            return "/messa.jsp";
        }

    }
    //查看订单详情
    public String getOrderDetail(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        String oid = request.getParameter("oid");
        //查询订单
        OrderService orderService=new OrderServiceImpl();
        Order order=orderService.findById(oid);
        //查询订单项
        List<OrderDetail> orderDetails=orderService.findDetail(oid);
        try {
            request.setAttribute("order", order);
            request.setAttribute("orderDetails",orderDetails);
            return "/orderDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单详情失败");
            return "/message.jsp";
        }
    }
}
